<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>发布消息</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <link rel="stylesheet" href="css/eleTree.css">
    <style>
        .layui-form-label{
            width: 110px;
        }
        @media screen and (max-width: 450px){
            .layui-form-item .layui-input-inline {
                margin-left: 140px;
            }
        }
        .layui-input-block{
            margin-left: 150px;
        }
        .eleTree-node.child-my-child-2{
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            发布消息
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
              <label class="layui-form-label">消息标题：</label>
              <div class="layui-input-inline">
                <input type="text" name="title" autocomplete="off" placeholder="请输入消息标题" class="layui-input" maxlength="20">
              </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发布到区域：</label>
                <div class="layui-input-block">
                    <div class="eleTree ele1"></div>
                </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">消息内容：</label>
              <div class="layui-input-block">
                <textarea name="content" placeholder="请输入消息内容" class="layui-textarea" maxlength="140"></textarea>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">确认</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="window.history.back()">取消</button>
              </div>
            </div>
        </form>
    </div>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>

    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'form', 'eleTree'], function() {
        var http = layui.http,
            form = layui.form,
            eleTree = layui.eleTree;

            
        //用户权限
        var el = '';
        getPermission();
        function getPermission(){
            // http.get('admin/region/listAllRegion',{},function(res){
            //     if(res.code == 200){
            //         var data = res.data;
                    el = eleTree.render({
                        elem: '.ele1',
                        url: TUrl + 'admin/region/listAllRegion',
                        method: 'get',
                        // data: data,
                        showCheckbox: true,
                        defaultExpandAll:true,
                        request: {     // 对后台返回的数据格式重新定义
                            name: "regionName",
                            key: "id",
                            children: "children",
                            checked: "checked",
                            disabled: "disabled",
                            isLeaf: "isLeaf",
                            level: "level"
                        },
                        response: {
                            statusName: "code",
                            statusCode: 200,
                            dataName: "data"
                        },
                        done: function(res) {
                            // console.log(res)
                            $(".child-my-child-2").hover(function(){
                                var nodedata = $(this).data('nodedata')
                                if(nodedata)
                                    tip_index=layer.tips(nodedata, this, {
                                        time:0
                                    });
                            },function(){
                                layer.close(tip_index);
                            });
                        }
                    });
            //     }
            // });
        }
        
        //监听提交
        form.on('submit(demo1)', function(data){
            // console.log(data.field)
            if($.trim(data.field.title) == "") {
                layer.msg('请输入消息标题')
                return
            }
            
            var checkedList = el.getChecked(false, false);
            // console.log(checkedList)
            var regionIdSet = [];
            for(var i=0;i<checkedList.length;i++){
                if(checkedList[i].level == 2)
                regionIdSet.push(checkedList[i].id);
            };
            if($.trim(data.field.content) == "") {
                layer.msg('请输入消息内容')
                return
            }
            if(regionIdSet.length == 0){
                http.myOpen({
                    title:'提示',
                    content:'<p style="text-align:center;margin-top:20px;padding:0 20px">未指定具体接收区域，消息将发至所有供应商、食堂、餐饮总部</p>',
                    area:['300px','160px'],
                    btn:['确定','取消'],
                    btn1:function(index, layero){
                        http.postJSON('admin/message/add',JSON.stringify({
                            title: data.field.title,
                            content: data.field.content,
                            regionIdSet: regionIdSet
                        }),function(res){
                            if(res.code == 200){
                                layer.msg('操作成功')
                                layer.close(index)
                                setTimeout(function(){
                                    window.history.back()
                                },500)
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                        
                    }
                });
            }else{
                http.postJSON('admin/message/add',JSON.stringify({
                    title: data.field.title,
                    content: data.field.content,
                    regionIdSet: regionIdSet
                }),function(res){
                    if(res.code == 200){
                        layer.msg('操作成功')
                        setTimeout(function(){
                            window.history.back()
                        },500)
                    }else{
                        layer.msg(res.msg)
                    }
                })

            }
        });
    })
</script>
</body>
</html>